
// The diffuzifier takes an array.
public class Defuzzifier {
  private double EPS = 1e-6;

  private double sum(double qc[]) {
    double sum = 0;
    for (int i = 0; i < qc.length; ++i) {
      sum += qc[i];
    }
    return sum;
  }

  private double centroid(double f[], double lower) {
    double t = 0;
    double x;
    for (int i = 0; i < f.length; ++i) {
      x = lower + i * MembershipFunction.RESOLUTION;
      t += x * f[i];
    }
    return t / sum(f);
  }

  // TODO
  public double defuzzify(double f[], double lower) {
    double output = centroid(f, lower);
    if (Math.abs(output) <= EPS) output = 0;  
    return output;
  }

}
